package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @package com.heima.admin.service.impl
 * @Date 2022/2/26
 * @project heima-leadnews
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 分页查询
     * @param sensitiveDTO
     * @return
     */
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult findByNameAndPage(SensitiveDTO sensitiveDTO) {
        if (sensitiveDTO==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        sensitiveDTO.checkParam();
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sensitiveDTO.getName())){
            queryWrapper.like(AdSensitive::getSensitives,sensitiveDTO.getName());
        }
        Page<AdSensitive> page = new Page<>(sensitiveDTO.getPage(),sensitiveDTO.getSize());
        IPage<AdSensitive> result = page(page, queryWrapper);
        ResponseResult pageResult = new PageResponseResult(sensitiveDTO.getPage(), sensitiveDTO.getSize(), result.getTotal(), result.getRecords());
        return pageResult;
    }

    /**
     * 添加
     * @param ads
     * @return
     */
    @Override
    public ResponseResult insertAdsensitive(AdSensitive ads) {
        if (ads==null||StringUtils.isBlank(ads.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, ads.getSensitives()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        ads.setCreatedTime(new Date());
        save(ads);

        return ResponseResult.okResult("添加成功");
    }

    /**
     * 修改
     * @param ads
     * @return
     */
    @Override
    public ResponseResult updateAdsensitive(AdSensitive ads) {
        if (ads==null||ads.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (StringUtils.isBlank(ads.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, ads.getSensitives()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        ads.setCreatedTime(new Date());
        this.updateById(ads);
        return ResponseResult.okResult("修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteAdsensitive(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdSensitive adsensitive = getById(id);
        if (adsensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);

        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult<List<String>> selectAllSensitive() {

        return ResponseResult.okResult(adSensitiveMapper.findAll());
    }
}
